package com.example.shop.common.convert;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;

@Component
public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
    @Override
    public LocalDateTime convert(String source) {
        return null;
    }
    public LocalDateTime formatterConvert(String source) {
        try {
            if(Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}",source)){
                return  LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
            if(Pattern.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}",source)){
                return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
            }
            if(Pattern.matches("\\d{4}年\\d{2}月\\d{2}日 \\d{2}时\\d{2}分\\d{2}秒",source)){
                return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
